package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.system.domain.SysSchoolStudent;
import com.ruoyi.system.mapper.SysSchoolStudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysSchoolProjectMapper;
import com.ruoyi.system.domain.SysSchoolProject;
import com.ruoyi.system.service.ISysSchoolProjectService;

/**
 * 项目管理Service业务层处理
 * 
 * @author 杨皓焜
 * @date 2021-02-06
 */
@Service
public class SysSchoolProjectServiceImpl implements ISysSchoolProjectService 
{
    @Autowired
    private SysSchoolProjectMapper sysSchoolProjectMapper;
    @Autowired
    private SysSchoolStudentMapper sysSchoolStudentMapper;

    int projectNum = 0;

    /**
     * 查询项目管理
     * 
     * @param projectId 项目管理ID
     * @return 项目管理
     */
    @Override
    public SysSchoolProject selectSysSchoolProjectById(Long projectId)
    {
        return sysSchoolProjectMapper.selectSysSchoolProjectById(projectId);
    }

    /**
     * 查询项目管理列表
     * 
     * @param sysSchoolProject 项目管理
     * @return 项目管理
     */
    @Override
    public List<SysSchoolProject> selectSysSchoolProjectList(SysSchoolProject sysSchoolProject)
    {
        return sysSchoolProjectMapper.selectSysSchoolProjectList(sysSchoolProject);
    }

    /**
     * 新增项目管理
     * 
     * @param sysSchoolProject 项目管理
     * @return 结果
     */
    @Override
    public int insertSysSchoolProject(SysSchoolProject sysSchoolProject)
    {
        SysSchoolStudent student = sysSchoolStudentMapper.selectSysSchoolStudentById(sysSchoolProject.getStudentId());
        Long classId = student.getClassId();
        sysSchoolProject.setClassId(classId);
        verifiProject(sysSchoolProject, projectNum);
        return sysSchoolProjectMapper.insertSysSchoolProject(sysSchoolProject);
    }

    /**
     * 项目查重
     * @param sysSchoolProject
     * @param i
     */
    private void verifiProject(SysSchoolProject sysSchoolProject, Integer i) {
        if (sysSchoolProject.getProjectId() == null) {
            SysSchoolProject schoolProject = sysSchoolProjectMapper.findByName(sysSchoolProject.getProjectName());
            if (schoolProject != null) {
                throw new CustomException("当前项目名已存在");
            }else {
                Integer typeCount = sysSchoolProjectMapper.findTypeByClassId(sysSchoolProject.getType(), sysSchoolProject.getClassId());
                System.out.println(typeCount);
                if (typeCount >= i) {
                    throw new CustomException("当前项目类型已超过限制");
                }
            }
        }else {
            Integer typeCount = sysSchoolProjectMapper.findTypeByClassId(sysSchoolProject.getType(), sysSchoolProject.getClassId());
            System.out.println(typeCount);
            if (typeCount > i) {
                throw new CustomException("当前项目类型已超过限制");
            }
        }
    }

    /**
     * 修改项目管理
     * 
     * @param sysSchoolProject 项目管理
     * @return 结果
     */
    @Override
    public int updateSysSchoolProject(SysSchoolProject sysSchoolProject)
    {
        verifiProject(sysSchoolProject,projectNum);
        return sysSchoolProjectMapper.updateSysSchoolProject(sysSchoolProject);
    }

    /**
     * 批量删除项目管理
     * 
     * @param projectIds 需要删除的项目管理ID
     * @return 结果
     */
    @Override
    public int deleteSysSchoolProjectByIds(Long[] projectIds)
    {
        return sysSchoolProjectMapper.deleteSysSchoolProjectByIds(projectIds);
    }

    /**
     * 删除项目管理信息
     * 
     * @param projectId 项目管理ID
     * @return 结果
     */
    @Override
    public int deleteSysSchoolProjectById(Long projectId)
    {
        return sysSchoolProjectMapper.deleteSysSchoolProjectById(projectId);
    }

    @Override
    public String setProjectNum(Integer num) {
        projectNum = num;
        return "设置成功";
    }
}
